package com.baizhi.service.impl;

import com.baizhi.mapper.GoodsMapper;
import com.baizhi.pojo.Count;
import com.baizhi.pojo.Goods;
import com.baizhi.pojo.GoodsExample;
import com.baizhi.entity.PageResult;
import com.baizhi.entity.QueryPageBean;
import com.baizhi.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<Goods> goodsList = null;
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        criteria.andStatusNotEqualTo((byte)0);
        if(queryPageBean.getQueryString()!=null&&!"".equals(queryPageBean.getQueryString())) {
            if (queryPageBean.getQueryString().equals("停用")){
                criteria.andStatusEqualTo((byte)1);
            }else if (queryPageBean.getQueryString().equals("启用")) {
                criteria.andStatusEqualTo((byte) 2);
            }else if (queryPageBean.getQueryString().equals("发布")){
                criteria.andStatusEqualTo((byte)3);
            }else if (queryPageBean.getQueryString().equals("上架")){
                criteria.andStatusEqualTo((byte)7);
            }else if (queryPageBean.getQueryString().equals("下架")){
                criteria.andStatusEqualTo((byte)8);
            }else if (queryPageBean.getQueryString().equals("待审核")){
                criteria.andStatusEqualTo((byte)4);
            }else if (queryPageBean.getQueryString().equals("审核通过")){
                criteria.andStatusEqualTo((byte)5);
            }else if (queryPageBean.getQueryString().equals("审核不通过")){
                criteria.andStatusEqualTo((byte)6);
            }else {
                criteria.andNameLike("%"+queryPageBean.getQueryString()+"%");
            }
        }
        goodsList = goodsMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(goodsList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void insertGoods(Goods goods) {
        Date date = new Date();
        goods.setUpdated(date);
        goods.setCreated(date);
        goodsMapper.insertSelective(goods);
    }

    @Override
    public Goods findById(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer edit(Goods goods) {
        if (goods.getStatus()==1||goods.getStatus()==6||goods.getStatus()==8){
            Date date = new Date();
            goods.setUpdated(date);
            int num = goodsMapper.updateByPrimaryKeySelective(goods);
            return num ;
        }
      return 0;
    }

    @Override
    public Integer delete(Long id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        if (goods.getStatus()==1){
            int num = goodsMapper.deleteByPrimaryKey(id);
            return num;
        }
        return 0;
    }

    @Override
    public Integer editStatus(Long id, Integer status) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        goods.setStatus(status);
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public Integer updateStatusAndBecause(Integer id, Integer status, String b) {
        Goods goods = goodsMapper.selectByPrimaryKey(Long.valueOf(id.toString()));
        goods.setStatus(status);
        if (status == 5){
            goods.setPutaway(b);
        }else {
            goods.setSoldout(b);
        }
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public List<Count> count() {
        //待审核 4、上架5、下架6  ,审核通过 7、审核不通过 8
        List<Count> status = goodsMapper.count();
        for (Count statistics : status) {
            if (statistics.getName().equals("4")) {
                statistics.setName("待审核");
            }else if (statistics.getName().equals( "5")){
                statistics.setName("上架");
            }else if (statistics.getName().equals( "6")){
                statistics.setName("下架");
            }else if (statistics.getName().equals( "7")){
                statistics.setName("审核通过");
            }else if (statistics.getName().equals("8")){
                statistics.setName("审核不通过");
            }
        }
        return status;
    }

}
